package biz.obake.team.android;

import android.graphics.Bitmap;
import android.graphics.Rect;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class e {

    /* renamed from: a  reason: collision with root package name */
    private Bitmap f1536a;

    /* renamed from: b  reason: collision with root package name */
    private boolean f1537b;

    /* renamed from: c  reason: collision with root package name */
    private Rect f1538c = new Rect();

    /* renamed from: d  reason: collision with root package name */
    private int[] f1539d;
    private int[] e;
    private int[] f;
    private int g = 320;

    public e(Bitmap bitmap) {
        this.f1536a = bitmap;
        this.f1537b = false;
        b();
        l();
        m();
        j();
        k();
        h();
        this.f1537b = true;
    }

    private int a(int[] iArr) {
        int i = iArr[0];
        if (f(i)) {
            for (int f2 : iArr) {
                if (!f(f2)) {
                    return 1;
                }
            }
            return 0;
        }
        for (int i2 : iArr) {
            if (i2 != i) {
                return 1;
            }
        }
        return i;
    }

    private void b() {
        int width = this.f1536a.getWidth();
        int height = this.f1536a.getHeight();
        if (width < 3) {
            throw new Exception("9patch bitmap width should be >= 3.");
        } else if (height < 3) {
            throw new Exception("9patch bitmap height should be >= 3.");
        }
    }

    private boolean e(int i) {
        return i == -16777216;
    }

    private boolean f(int i) {
        return (i & -16777216) == 0;
    }

    private boolean g(int i) {
        return i == -1;
    }

    private void h() {
        int i;
        int i2;
        int width = this.f1536a.getWidth();
        int height = this.f1536a.getHeight();
        int[] iArr = new int[257];
        if (this.f1539d[0] > 0) {
            iArr[0] = 0;
            i = 1;
        } else {
            i = 0;
        }
        int[] iArr2 = this.f1539d;
        System.arraycopy(iArr2, 0, iArr, i, iArr2.length);
        int[] iArr3 = this.f1539d;
        int length = i + iArr3.length;
        int i3 = width - 2;
        if (iArr3[iArr3.length - 1] < i3) {
            iArr[length] = i3;
            length++;
        }
        int[] iArr4 = new int[length];
        System.arraycopy(iArr, 0, iArr4, 0, length);
        int[] iArr5 = new int[257];
        if (this.e[0] > 0) {
            iArr5[0] = 0;
            i2 = 1;
        } else {
            i2 = 0;
        }
        int[] iArr6 = this.e;
        System.arraycopy(iArr6, 0, iArr5, i2, iArr6.length);
        int[] iArr7 = this.e;
        int length2 = i2 + iArr7.length;
        int i4 = height - 2;
        if (iArr7[iArr7.length - 1] < i4) {
            iArr5[length2] = i4;
            length2++;
        }
        int[] iArr8 = new int[length2];
        System.arraycopy(iArr5, 0, iArr8, 0, length2);
        int i5 = length - 1;
        int i6 = length2 - 1;
        this.f = new int[(i5 * i6)];
        int i7 = 0;
        while (i7 < i6) {
            int i8 = iArr8[i7];
            int i9 = i7 + 1;
            int i10 = iArr8[i9] - i8;
            int i11 = 0;
            while (i11 < i5) {
                int i12 = iArr4[i11];
                int i13 = i11 + 1;
                int i14 = iArr4[i13] - i12;
                int[] iArr9 = new int[(i14 * i10)];
                this.f1536a.getPixels(iArr9, 0, i14, i12 + 1, i8 + 1, i14, i10);
                this.f[(i5 * i7) + i11] = a(iArr9);
                i11 = i13;
            }
            i7 = i9;
        }
    }

    private int[] i(int[] iArr) {
        try {
            int[] iArr2 = new int[255];
            boolean z = false;
            int i = 0;
            for (int i2 = 0; i2 < iArr.length; i2++) {
                int i3 = iArr[i2];
                if (!e(i3)) {
                    if (!g(i3)) {
                        if (!f(i3)) {
                            throw new Exception("9patch bitmap ___ should consist of black, white or transparent pixels only.");
                        }
                    }
                    if (z) {
                        iArr2[i] = i2;
                        i++;
                        z = false;
                    }
                } else if (!z) {
                    iArr2[i] = i2;
                    i++;
                    z = true;
                }
            }
            if (z) {
                iArr2[i] = iArr.length;
                i++;
            }
            if (i != 0) {
                int[] iArr3 = new int[i];
                System.arraycopy(iArr2, 0, iArr3, 0, i);
                return iArr3;
            }
            throw new Exception("9 path ___ should contain one or more black pixels.");
        } catch (ArrayIndexOutOfBoundsException unused) {
            throw new Exception("9 patch ___ exceeds 255 divs.");
        }
    }

    private void j() {
        int width = this.f1536a.getWidth();
        int i = width - 2;
        int[] iArr = new int[i];
        this.f1536a.getPixels(iArr, 0, width, 1, this.f1536a.getHeight() - 1, i, 1);
        int i2 = 0;
        this.f1538c.left = this.f1539d[0];
        while (true) {
            if (i2 >= i) {
                break;
            } else if (e(iArr[i2])) {
                this.f1538c.left = i2;
                break;
            } else {
                i2++;
            }
        }
        this.f1538c.right = i - this.f1539d[1];
        int i3 = i - 1;
        for (int i4 = i3; i4 >= 0; i4--) {
            if (e(iArr[i4])) {
                this.f1538c.right = i3 - i4;
                return;
            }
        }
    }

    private void k() {
        int width = this.f1536a.getWidth();
        int height = this.f1536a.getHeight() - 2;
        int[] iArr = new int[height];
        this.f1536a.getPixels(iArr, 0, 1, width - 1, 1, 1, height);
        int i = 0;
        this.f1538c.top = this.e[0];
        while (true) {
            if (i >= height) {
                break;
            } else if (e(iArr[i])) {
                this.f1538c.top = i;
                break;
            } else {
                i++;
            }
        }
        this.f1538c.bottom = height - this.e[1];
        int i2 = height - 1;
        for (int i3 = i2; i3 >= 0; i3--) {
            if (e(iArr[i3])) {
                this.f1538c.bottom = i2 - i3;
                return;
            }
        }
    }

    private void l() {
        int width = this.f1536a.getWidth();
        int i = width - 2;
        int[] iArr = new int[i];
        this.f1536a.getPixels(iArr, 0, width, 1, 0, i, 1);
        try {
            this.f1539d = i(iArr);
        } catch (Exception e2) {
            throw new Exception(e2.getMessage().replace("___", "top border"));
        }
    }

    private void m() {
        int height = this.f1536a.getHeight() - 2;
        int[] iArr = new int[height];
        this.f1536a.getPixels(iArr, 0, 1, 0, 1, 1, height);
        try {
            this.e = i(iArr);
        } catch (Exception e2) {
            throw new Exception(e2.getMessage().replace("___", "left border"));
        }
    }

    public byte[] c() {
        if (this.f1537b) {
            ByteBuffer putInt = ByteBuffer.allocate(32 + (this.f1539d.length * 4) + (this.e.length * 4) + (this.f.length * 4)).order(ByteOrder.nativeOrder()).put((byte) 1).put((byte) this.f1539d.length).put((byte) this.e.length).put((byte) this.f.length).putInt(0).putInt(0).putInt(this.f1538c.left).putInt(this.f1538c.right).putInt(this.f1538c.top).putInt(this.f1538c.bottom).putInt(0);
            for (int putInt2 : this.f1539d) {
                putInt.putInt(putInt2);
            }
            for (int putInt3 : this.e) {
                putInt.putInt(putInt3);
            }
            for (int putInt4 : this.f) {
                putInt.putInt(putInt4);
            }
            return putInt.array();
        }
        throw new Exception("9 patch bitmap was not parsed successfully.");
    }

    public Bitmap d() {
        int width = this.f1536a.getWidth();
        int height = this.f1536a.getHeight();
        if (width < 3 || height < 3) {
            return null;
        }
        Bitmap bitmap = this.f1536a;
        Bitmap createBitmap = Bitmap.createBitmap(bitmap, 1, 1, bitmap.getWidth() - 2, this.f1536a.getHeight() - 2);
        createBitmap.setDensity(this.g);
        return createBitmap;
    }

    public e n(int i) {
        if (i > 0) {
            this.g = i;
        }
        return this;
    }
}
